package com.rw.controller;

import com.rw.entity.User;
import com.rw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @RestController注解放类上面使用，表示的是这个类下面的所有方法返回的都是json
 * @RestController = @Controller +@ResponseBody
 * @author rabit
 *
 */

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("apiLogin")
    public String login(String uname,String pwd) {
        System.out.println("LoginController.login"+uname+"----"+pwd);
        User user = userService.getLogin(uname,pwd);
        System.out.println(user);
        if (user == null){
            return "error.jsp";
        }
        return "success.jsp";
    }

    @RequestMapping("demo")
    public String getDemo(Model model) {
        System.out.println("LoginController.getDemo");
        model.addAttribute("str","getDemo设置的值");
        return "forward:/user/demo2";
    }
    @RequestMapping("demo2")
    public String getDemo2() {
//        int i = 1/0;
        System.out.println("转发过来的demo2");
        return "success.jsp";
    }

    @RequestMapping("demo3")
    public String getDemo3() {
//        int i = 1/0;
        System.out.println("转发过来的demo3");
        return "success.jsp";
    }


    //@RequestMapping(value = "json",method = RequestMethod.GET)

    @ResponseBody
    @GetMapping("json")
    public User getJson(){
        User user = new User();
        user.setUname("张三GET");
        user.setUid(1);
        user.setPwd("1234567");
        System.out.println(user);
        return user;
    }

    //@RequestMapping(value = "json",method = RequestMethod.POST)
   @PostMapping("json")
    @ResponseBody
    public User getJson2(){
        User user = new User();
        user.setUname("张三POST");
        user.setUid(1);
        user.setPwd("1234567");
        System.out.println(user);
        return user;
    }

}
